<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="com.onlineauction.helpers.Utili"%>
<%@ page import="com.onlineauction.entities.Review"%>
<%@ page import="com.onlineauction.entities.User"%>
<%@ page import="com.onlineauction.helpers.Constants"%>
<%@ page import="java.util.List"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Review A Seller</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="rating/jquery.rating.css" />
<script src="rating/jquery.js"></script>
<script src="rating/jquery.form.js"></script>
<script src="rating/jquery.rating.pack.js"></script>
<%
	String reviewee = (String) request.getAttribute(Constants.REVIEWEE);
	if (reviewee == null || reviewee.isEmpty()) {
		response.sendRedirect("/Home");
	}
	List<Review> reviews = (List<Review>) request
			.getAttribute("reviews");
	String averageRating = Utili.getAverageRating(reviews);
	int numberOfreview = reviews.size();
%>
<script type="text/javascript">// <![CDATA[
		$(document).ready(function() {
		$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
		setInterval(function() {
		$(#test).load("/review?reviewee="+reviewee);
		}, 5000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
		});
	</script>
</head>


<body>
<div id=test></div>
	<div id="SellerInfo">
		<h2>
			Reviews on Seller:
			<%=reviewee%>
		</h2>
		<hr>
		<h3>
			Average Rating:<%
			out.print(averageRating);
		%>
		</h3>
		<p>
			based on
			<%
			out.print(reviews.size());
		%>
			ratings
		</p>
	</div>
	<hr />
	<div id="viewComents">
		<%
			for (int i=0; i<reviews.size();i++) {
				Review review = reviews.get(i);
				int rating= review.getRating();
				String name = "start"+i;
		%>
		<b><%=review.getTitle()%></b><br />
		<input name= <%=name %> type="radio" class="star" value="1" disabled="disabled" <%if(rating>0){out.print("checked=\"checked\"");} %>/>
		<input name=<%=name %> type="radio" class="star" value="2" disabled="disabled" <%if(rating>1){out.print("checked=\"checked\"");} %>/>
		<input name=<%=name %> type="radio" class="star" value="3" disabled="disabled" <%if(rating>2){out.print("checked=\"checked\"");} %>/>
		<input name=<%=name %> type="radio" class="star" value="4" disabled="disabled" <%if(rating>3){out.print("checked=\"checked\"");} %>/>
		<input name=<%=name %> type="radio" class="star" value="5" disabled="disabled" <%if(rating>4){out.print("checked=\"checked\"");} %>/>
		<span>by <%=review.getReviewer() %> -- <span style="color:#A0A0A0"><%=review.getTimestamp().toString()%></span></span><br/>
		<p><%=review.getComment() %></p>
		<hr>
		<%} %>
	</div>

	<%
		String username = (String) request.getSession(true).getAttribute(Constants.USERNAME);
	    User user = new User(username);
	    if (user.getUsertype() == Constants.Usertype.Buyer) {
	%>
	<div id="AddingReview">
		<H3>Write A New Review:</H3>
		<form action="review" method="post">
			<table>

				<tr>
					<td>Title:</td> 
					<td align="left"><input type="text" name=<%=Constants.TITLE%> /></td>
				</tr>
				<tr>
					<td >Rating:</td>
					<td>
					<input name=<%=Constants.RATING%> type="radio" class="star" value="1" /> 
					<input name=<%=Constants.RATING%> type="radio" class="star" value="2" /> 
					<input name=<%=Constants.RATING%> type="radio" class="star" value="3" /> 
					<input name=<%=Constants.RATING%> type="radio" class="star" value="4" /> 
					<input name=<%=Constants.RATING%> type="radio" class="star" value="5" />
					</td>
				</tr>
				<tr>
					<td colspan="2">Comment:<br/>
					<textarea name=<%=Constants.COMMENT%> cols="35" rows="5"></textarea>
					</td>
				</tr>

				<tr>
					<td colspan="2" align="right">
					<input type="submit" value="Submit" /> 
					 <input
						type="hidden" name=<%=Constants.REVIEWEE%> value=<%=reviewee%> />
					</td>
				</tr>
			</table>
		</form>
	</div>	
	<%
	    }
	%>
	
	
	
</body>
</html>